"""
Module to filter signals

Functions
---------
`lpfilter`: Low pass filter.

`hpfilter`: Hight pass filter.

`bpfilter`: Band pass filter.

"""
#
# filter.py
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

from scipy.signal import firwin, lfilter

#--------------------------------------------------------------
# LP Filter
#--------------------------------------------------------------
def lpfilter(samples, sample_rate, cutoff_hz, numtaps):
    # The Nyquist rate of the signal.
    nyq_rate = sample_rate / 2.

    # Use firwin to create a lowpass FIR filter
    fir_coeff = firwin(numtaps, cutoff_hz/nyq_rate)

    # Use lfilter to filter the signal with the FIR filter
    filtered_signal = lfilter(fir_coeff, 1.0, samples)
    return (filtered_signal, fir_coeff, nyq_rate)

#--------------------------------------------------------------
# HP Filter
#--------------------------------------------------------------
def hpfilter(samples, sample_rate, cutoff_hz, numtaps):
    # The Nyquist rate of the signal.
    nyq_rate = sample_rate / 2.

    # Use firwin to create a lowpass FIR filter
    fir_coeff = firwin(numtaps, cutoff_hz/nyq_rate, pass_zero=False)

    # Use lfilter to filter the signal with the FIR filter
    filtered_signal = lfilter(fir_coeff, 1.0, samples)
    return (filtered_signal, fir_coeff, nyq_rate)

#--------------------------------------------------------------
# BP Filter
#--------------------------------------------------------------
def bpfilter(samples, sample_rate, cutoff_L_hz, cutoff_R_hz, numtaps):
    # The Nyquist rate of the signal.
    nyq_rate = sample_rate / 2.

    # Length of the filter (number of coefficients, i.e. the filter order + 1)
    numtaps = 51

    # Use firwin to create a highpass FIR filter
    fir_coeff = firwin(numtaps, [cutoff_L_hz/nyq_rate, cutoff_R_hz/nyq_rate], pass_zero=False)

    # Use lfilter to filter the signal with the FIR filter
    filtered_signal = lfilter(fir_coeff, 1.0, samples)
    return (filtered_signal, fir_coeff, nyq_rate)
